package com.youbug.common.config;

import com.youbug.common.interceptor.AuthInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private AuthInterceptor authInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(authInterceptor).addPathPatterns("/**").excludePathPatterns("/file/**");
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

       String ASSETS_PATH = System.getProperty("os.name").toLowerCase().startsWith("win") ? "/file" : "";
        String PROJECT_PATH = System.getProperty("user.dir") + ASSETS_PATH;
        String RESOURCE_LOCATION = "file:" + PROJECT_PATH + "/file/";

        registry.addResourceHandler("/file/**")
                .addResourceLocations(RESOURCE_LOCATION);
    }
    /**
     * webSocket 服务端
     * @return
     */
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

}
